package com.example.administrator.androidros.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.example.administrator.androidros.R;
import com.example.administrator.androidros.adapter.SceneAddDeviceAdapter;
import com.example.administrator.androidros.base.BaseActivity;
import com.example.administrator.androidros.model.ApplianceModel;
import com.hhc.netty.DeviceModel;
import com.example.administrator.androidros.net.HttpUtils;
import com.example.administrator.androidros.utils.AgentConstants;
import com.example.administrator.androidros.utils.AgentSharedPreferences;
import com.example.administrator.androidros.utils.ServerConstants;
import com.example.administrator.androidros.widget.CustomTipDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class SceneAddDeviceActivity extends BaseActivity {
    @BindView(R.id.activity_scene_add_device_list)
    protected RecyclerView activity_scene_add_device_list;
    private String sceneMasterId = null;
    private SceneAddDeviceAdapter sceneAddDeviceAdapter;
    protected List<ApplianceModel> data = new ArrayList();
    private int mPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_scene_add_device);
    }

    @Override
    protected void initData() {
        getIntentData();
        initCommonHeader();
        initView();
    }

    @Override
    protected void onFailure(int requestCode, Object object) {
        showToast(R.string.common_response_failure);
        stopTipDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        stopTipDialog();
        if (object != null) {
            switch (requestCode) {
                case 1001:
                    ApplianceModel[] deviceModels = (ApplianceModel[]) HttpUtils.getData(object, ApplianceModel[].class);
                    if (deviceModels != null) {
                        data = Arrays.asList(deviceModels);
                        sceneAddDeviceAdapter.setNewData(data);
                    }
                    break;
                case 1002:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        showToast(HttpUtils.getResultMessage(object));
                        sceneAddDeviceAdapter.getDataList().remove(mPosition);
                        sceneAddDeviceAdapter.notifyItemRemoved(mPosition);//推荐用这个
                        if (mPosition != (sceneAddDeviceAdapter.getDataList().size())) { // 如果移除的是最后一个，忽略 注意：这里的mDataAdapter.getDataList()不需要-1，因为上面已经-1了
                            sceneAddDeviceAdapter.notifyItemRangeChanged(mPosition, sceneAddDeviceAdapter.getDataList().size() - mPosition);
                        }
                    } else {
                        showToast(HttpUtils.getResultMessage(object));
                    }
                    break;
                default:
                    break;
            }
        }
    }

    protected void getIntentData() {
        // 获取意图对象
        Intent intent = getIntent();
        //获取传递的值
        sceneMasterId = intent.getStringExtra("sceneMasterId");
    }

    protected void initCommonHeader() {
        configCommonHeader(this, getString(R.string.device_add), false);
    }

    protected void initView() {
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        activity_scene_add_device_list.setLayoutManager(mLayoutManager);
        sceneAddDeviceAdapter = new SceneAddDeviceAdapter(this, data);
        activity_scene_add_device_list.setAdapter(sceneAddDeviceAdapter);
        sceneAddDeviceAdapter.setOnItemClickListener(new SceneAddDeviceAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                mPosition = position;
                String masterId = sceneAddDeviceAdapter.getDataList().get(position).getMasterId();
                addAppliance(masterId, sceneMasterId);
            }
        });
    }

    /**
     * 获取当前memberId下，设备列表中，sceneId为空的设备
     */
    public void getApplianceList() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberId", AgentSharedPreferences.getLastLoginId(mBaseActivity));
        jsonObject.put("sceneMasterId", "");
        jsonObject.put("type", "10");
        HttpUtils.post(mBaseActivity, 1001, ServerConstants.QUERY_APPLIANCE_LIST, jsonObject.toString(), mBaseActivity);
        startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.common_loading), true);
    }

    public void addAppliance(String masterId, String sceneMasterId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("masterId", masterId);
        jsonObject.put("sceneMasterId", sceneMasterId);
        HttpUtils.post(mBaseActivity, 1002, ServerConstants.ADD_SCENE_APPLIANCE, jsonObject.toString(), mBaseActivity);
        startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.common_loading), true);
    }

    @Override
    public void onResume() {
        super.onResume();
        getApplianceList();
    }
}
